package com.gitee.wsl.ext.set


fun <T> MutableSet<T>.push(item: T) = this.add(item)

fun <T> MutableSet<T>.removeFirst():T{
    val item = this.first()
    remove(item)
    return item
}

fun <T> MutableSet<T>.removeLast():T{
    val item = this.last()
    remove(item)
    return item
}

fun <T> MutableSet<T>.popOrNull(): T? = if(this.isNotEmpty()) this.removeLast() else null

fun <T> MutableSet<T>.pop(): T = if(this.isNotEmpty()) this.removeLast() else throw IndexOutOfBoundsException()

inline fun <T> MutableSet<T>.usePop(block: (T) -> Unit) = popOrNull()?.let(block)

fun <T> MutableSet<T>.peekOrNull(): T? = if(this.isNotEmpty()) this.last() else null

fun <T> MutableSet<T>.peek(): T = if(this.isNotEmpty()) this.last() else throw IndexOutOfBoundsException()

inline fun <T> MutableSet<T>.usePeek(block: (T) -> Unit) = peekOrNull()?.let(block)

fun <T> MutableSet<T>.pollFirst(): T = if(this.isNotEmpty()) removeFirst() else throw IndexOutOfBoundsException()
